
package scalaExec.gui

import scala.collection.immutable.TreeMap
import scalaExec.Interpreter.GlobalValues

// collects the defined variables and their values in GlobalValues.bindedVars
// and displays them in the workspace variables table of ScalaLab
// Also, we add vars to AutoCompletionWorkspace table

object workspaceVarsCollector  {

  def  getAllVars = {

var vecOfVars = new java.util.Vector[String]()

scalaExec.gui.AutoCompletionWorkspace.scanVars.clear  // clear variables for autocompletion

GlobalValues.bindedVarsTypes = TreeMap()
var boundVarNames =  GlobalValues.globalInterpreter.allValueGeneratingNames
// the current  bound names  for vals and vars (i.e. not functions)
       
var  columnNames = new java.util.Vector[String]()
columnNames.add("Vars")
columnNames.add("Types")
var varCnt=0;
var boundVarsCnt=boundVarNames.length
var k=0
 while  (k< boundVarsCnt) {
            var currentVar = boundVarNames(k)
            var currentVarStr = currentVar.toString
            
if (currentVarStr.indexOf("res")!=0 && currentVarStr.equals("settings") == false && currentVarStr.indexOf("synthvar") != 0 && currentVarStr.indexOf("$") == -1 )  {  // not an automatically created variable
            if (GlobalValues.defaultImplicitsNames.contains(currentVarStr)== false) {
   var typeRes = GlobalValues.globalInterpreter.typeForIdent(currentVarStr).getOrElse("Undefined")

   var resType = typeRes.toString
    GlobalValues.bindedVarsTypes += (currentVarStr -> resType)
    vecOfVars.add(currentVarStr)  // add for TAB autocompletion
    varCnt+=1
    }
 }
 k+=1
} 

GlobalValues.model = new javax.swing.table.DefaultTableModel(varCnt, 2)
GlobalValues.model.setColumnIdentifiers(columnNames)
 
 var elems = GlobalValues.bindedVarsTypes.elements
 k=0
 while (elems.hasNext) {
   var currElem = elems.next
   var currKey = currElem._1
   var currType = currElem._2
   GlobalValues.model.setValueAt(currKey, k, 0)  // variable's name
   GlobalValues.model.setValueAt(currType, k, 1)  // variable's type
    k += 1

}

vecOfVars
}
   



  
}
        
   